package com.study.mybatis.dao;

import com.study.mybatis.model.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

public interface UserDao {

    @Select("select count(1) from t_user where username = #{username} and password = #{password}")
    int finOneBtUserNameAndPwd(Map<String, String> paramMap);

    @Select("select * from t_user")
    @Results(id = "userResult", value = {
        @Result(column="id" ,javaType=Integer.class,jdbcType= JdbcType.INTEGER, property="id"),
            @Result(column = "create_time", javaType = Integer.class, jdbcType = JdbcType.INTEGER, property = "createTime"),
            @Result(column = "update_time", javaType = Integer.class, jdbcType = JdbcType.INTEGER, property = "updateTime"),
            @Result(column = "user_gid", javaType = String.class, jdbcType = JdbcType.VARCHAR, property = "userGid"),
            @Result(column = "username", javaType = String.class, jdbcType = JdbcType.VARCHAR, property = "username"),
            @Result(column = "password", javaType = String.class, jdbcType = JdbcType.VARCHAR, property = "password"),
    })
    List<User> findAll();

    @Select("select * from t_user where username=#{userName}")
    @ResultMap("userResult")
    User finOneByUserName(String userName);

    @Insert("insert into t_user(id, create_time, update_time, user_gid, username, password)\n" +
            "        values (null, #{createTime}, #{updateTime}, #{userGid}, #{username}, #{password})")
    int insert(User insertUser);

    @Select("<script>" +
            "select * from t_user where id in (" +
            "<foreach collection=\"list\" item=\"a\"  separator=\",\">\n" +
            "            #{a}\n" +
            " </foreach>"
            +")</script>")
    @ResultMap("userResult")
    List<User> findBtIds(List<Integer> ids);
}
